﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using XStudio.XCRF.Data.Entities.CureInfo;
using XStudio.XCRF.Data.Entities.TongueAndPulse;
using XStudio.XCRF.Data.Enums;
using XStudio.XCRF.Web.Models.Enums;

namespace XStudio.XCRF.Web.Export
{
    public class CureInfoPanelCreator
    {

        public Panel createCureInfoPanel(CureInfo cureInfo)
        {
            Panel panel = new Panel();

            if (cureInfo == null)
                return panel;

            Panel p_title = new Panel();
            p_title.Width = Unit.Parse("100%");
            p_title.HorizontalAlign = HorizontalAlign.Center;
            Label lbl_general = new Label();
            lbl_general.Font.Bold = true;
            lbl_general.Font.Size = FontUnit.Larger;
            lbl_general.Text = "治疗";
            p_title.Controls.Add(lbl_general);
            panel.Controls.Add(p_title);

            Label lbl_cureDate = new Label();
            lbl_cureDate.Text = "日期：" + cureInfo.Time.ToShortDateString();
            panel.Controls.Add(lbl_cureDate);

            Table table = new Table();
            table.Width = Unit.Parse("100%");
            table.CellPadding = 0;
            table.CellSpacing = 0;
            

            TableRow complaintRow = new TableRow();
            complaintRow.Cells.Add(createCell("主诉：" + cureInfo.Complaint, true, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(complaintRow);

            TableRow symptomRow = new TableRow();
            symptomRow.Cells.Add(createCell("症状：" + cureInfo.Symptom, true, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(symptomRow);

            TableRow vitalSignRow = new TableRow();
            vitalSignRow.Cells.Add(createCell("体征：" + cureInfo.VitalSign, true, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(vitalSignRow);

            TableRow tongueAndPulseRow = new TableRow();
            Table tongueAndPulseTable = new Table();
            tongueAndPulseTable.Width = Unit.Parse("100%");
            TableCell tongueAndPulseCell = createCell("", true, Unit.Parse("100%"), HorizontalAlign.Left);
            tongueAndPulseCell.Controls.Clear();
            tongueAndPulseCell.Controls.Add(tongueAndPulseTable);
            tongueAndPulseRow.Cells.Add(tongueAndPulseCell);
            table.Rows.Add(tongueAndPulseRow);

            //舌象
            TableRow tongueColorRow = new TableRow();
            tongueColorRow.Cells.Add(createCell("舌象", false, Unit.Parse("10%"), HorizontalAlign.Left, 1, 5));
            String tongueColor = "舌色：";
            tongueColor += TongueColorTypeInfo.GetName(cureInfo.TongueColor) + "  " + cureInfo.TongueColorOther;
            tongueColorRow.Cells.Add(createCell(tongueColor, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(tongueColorRow);
            TableRow tongueShapeRow = new TableRow();
            String tongueShape = "舌形：";
            tongueShape += TongueShapeTypeInfo.GetName(cureInfo.TongueShape) + "  " + cureInfo.TongueShapeOther;
            tongueShapeRow.Cells.Add(createCell(tongueShape, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(tongueShapeRow);
            TableRow tongueStatusRow = new TableRow();
            String tongueStatus = "舌态：";
            tongueStatus += TongueShapeTypeInfo.GetName(cureInfo.TongueStatus) + "  " + cureInfo.TongueStatusOther;
            tongueStatusRow.Cells.Add(createCell(tongueStatus, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(tongueStatusRow);
            TableRow furCharacterRow = new TableRow();
            String furCharacter = "苔质：";
            furCharacter += FurCharacterTypeInfo.GetName(cureInfo.FurCharacter) + "  " + cureInfo.FurCharacterOther;
            furCharacterRow.Cells.Add(createCell(furCharacter, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(furCharacterRow);
            TableRow furColorRow = new TableRow();
            String furColor = "苔色：";
            furColor += FurColorTypeInfo.GetName(cureInfo.FurColor) + "  " + cureInfo.FurColorOther;
            furColorRow.Cells.Add(createCell(furColor, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(furColorRow);
            //脉象
            TableRow pulseFloatRow = new TableRow();
            pulseFloatRow.Cells.Add(createCell("脉象", false, Unit.Parse("10%"), HorizontalAlign.Left, 1, 6));
            String pulseFloat = "浮脉类：";
            pulseFloat += FloatingPulseClassTypeInfo.GetName(cureInfo.FloatingPulseClass) + "  " + cureInfo.FloatingPulseClassOther;
            pulseFloatRow.Cells.Add(createCell(pulseFloat, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(pulseFloatRow);
            TableRow pulseDeepsRow = new TableRow();
            String pulseDeeps = "沉脉类：";
            pulseDeeps += DeepPulseClassTypeInfo.GetName(cureInfo.DeepPulseClass) + "  " + cureInfo.DeepPulseClassOther; 
            pulseDeepsRow.Cells.Add(createCell(pulseDeeps, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(pulseDeepsRow);
            TableRow pulseSlowRow = new TableRow();
            String pulseSlow = "迟脉象：";
            pulseSlow += SlowPulseClassTypeInfo.GetName(cureInfo.SlowPulseClass) + "  " + cureInfo.SlowPulseClassOther;
            pulseSlowRow.Cells.Add(createCell(pulseSlow, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(pulseSlowRow);
            TableRow pulseRapidRow = new TableRow();
            String pulseRapid = "实脉象：";
            pulseRapid += RapidPulseClassTypeInfo.GetName(cureInfo.RapidPulseClass) + "  " + cureInfo.RapidPulseClassOther;
            pulseRapidRow.Cells.Add(createCell(pulseRapid, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(pulseRapidRow);
            TableRow pulseFeebleRow = new TableRow();
            String pulseFeeble = "虚脉象：";
            pulseFeeble += FeeblePulseClassTypeInfo.GetName(cureInfo.FeeblePulseClass) + "  " + cureInfo.FeeblePulseClassOther; 
            pulseFeebleRow.Cells.Add(createCell(pulseFeeble, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(pulseFeebleRow);
            TableRow pulseExcessRow = new TableRow();
            String pulseExcess = "实脉象：";
            pulseExcess += ExcessPulseClassTypeInfo.GetName(cureInfo.ExcessPulseClass) + "  " + cureInfo.ExcessPulseClassOther; 
            pulseExcessRow.Cells.Add(createCell(pulseExcess, false, Unit.Parse("90%"), HorizontalAlign.Left));
            tongueAndPulseTable.Rows.Add(pulseExcessRow);

            //中医辨证
            TableRow diseaseRow = new TableRow();
            TableCell diseaseCell = createCell("", true, Unit.Parse("100%"), HorizontalAlign.Left);
            diseaseCell.Controls.Clear();
            Table diseaseTable = new Table();
            diseaseTable.Width = Unit.Parse("100%");
            diseaseCell.Controls.Add(diseaseTable);
            diseaseRow.Cells.Add(diseaseCell);
            table.Rows.Add(diseaseRow);
            TableRow primarySymsRow = new TableRow();
            primarySymsRow.Cells.Add(createCell("中医辨证分型：", false, Unit.Parse("15%"), HorizontalAlign.Left ,1,2));
            String primarySyms = "主症：";
            foreach (Disease d in cureInfo.PrimarySyms)
                primarySyms += d.Name + "；";
            primarySymsRow.Cells.Add(createCell(primarySyms, false, Unit.Parse("85%"), HorizontalAlign.Left));
            diseaseTable.Rows.Add(primarySymsRow);
            TableRow subSymsRow = new TableRow();
            String subSyms = "主症：";
            foreach (Disease d in cureInfo.SubSyms)
                subSyms += d.Name + "；";
            subSymsRow.Cells.Add(createCell(subSyms, false, Unit.Parse("85%"), HorizontalAlign.Left));
            diseaseTable.Rows.Add(subSymsRow);

            //用药
            TableRow herbsRow = new TableRow();
            herbsRow.Cells.Add(createCell("用药：" + cureInfo.Herbs, true, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(herbsRow);

            TableRow reactionRow = new TableRow();
            reactionRow.Cells.Add(createCell("不良反应：" + cureInfo.Reaction, true, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(reactionRow);

            TableRow transRow = new TableRow();
            transRow.Cells.Add(createCell("复发转移情况：" + cureInfo.Trans.ToString(), true, Unit.Parse("100%"), HorizontalAlign.Left));
            table.Rows.Add(transRow);

            TableRow chemoRow = new TableRow();
            TableCell chemosCell = createCell("", true, Unit.Parse("100%"), HorizontalAlign.Center);
            chemosCell.ColumnSpan = 4;
            chemoRow.Controls.Add(chemosCell);
            Label lbl_Chemos = new Label();
            lbl_Chemos.Text = "化疗";
            chemosCell.Controls.Add(lbl_Chemos);
            chemosCell.Controls.Add(createChemosTable(cureInfo.Chemos));
            table.Rows.Add(chemoRow);

            TableRow radioRow = new TableRow();
            TableCell RadiosCell = createCell("", true, Unit.Parse("100%"), HorizontalAlign.Center);
            RadiosCell.ColumnSpan = 4;
            radioRow.Cells.Add(RadiosCell);
            Label lbl_Radios = new Label();
            lbl_Radios.Text = "放疗";
            RadiosCell.Controls.Add(lbl_Radios);
            RadiosCell.Controls.Add(createRadiosTable(cureInfo.Radios));
            table.Rows.Add(radioRow);

            TableRow otherCureRow = new TableRow();
            TableCell OtherCuresCell = createCell("", true, Unit.Parse("100%"), HorizontalAlign.Center);
            OtherCuresCell.ColumnSpan = 4;
            otherCureRow.Cells.Add(OtherCuresCell);
            Label lbl_OtherCures = new Label();
            lbl_OtherCures.Text = "其他";
            OtherCuresCell.Controls.Add(lbl_OtherCures);
            OtherCuresCell.Controls.Add(createBasicOtherCuresTable(cureInfo.OtherCures));
            table.Rows.Add(otherCureRow);

            panel.Controls.Add(table);

            Panel p_evaluate = new Panel();
            p_evaluate.HorizontalAlign = HorizontalAlign.Center;
            p_evaluate.Width = Unit.Parse("100%");
            Label lbl_evaluate = new Label();
            lbl_evaluate.Text = "疗效评估";
            lbl_evaluate.Font.Bold = true;
            p_evaluate.Controls.Add(lbl_evaluate);
            panel.Controls.Add(p_evaluate);

            Table evaluateTable = new Table();
            evaluateTable.CellSpacing = 0;
            evaluateTable.CellPadding = 0;
            evaluateTable.Width = Unit.Parse("100%");
            TableRow row1 = new TableRow();
            row1.Cells.Add(createCell("KPS评分：" + cureInfo.KPS.ToString(), true, Unit.Parse("50%"), HorizontalAlign.Left));
            row1.Cells.Add(createCell("ECOG评分：" + cureInfo.ECOG.ToString(), true, Unit.Parse("50%"), HorizontalAlign.Left));
            evaluateTable.Rows.Add(row1);
            TableRow row2 = new TableRow();
            row2.Cells.Add(createCell("生命质量评分：" + cureInfo.QOL.ToString(), true, Unit.Parse("50%"), HorizontalAlign.Left,2,1));
            evaluateTable.Rows.Add(row2);
            panel.Controls.Add(evaluateTable);

            return panel;
        }

        //中成药
        private Table createInjectsTable(ICollection<Inject> injects)
        {
            Table table = new Table();
            table.Width = Unit.Parse("100%");

            if (injects == null || injects.Count < 1)
                return table;
            foreach (Inject i in injects)
            {
                TableRow row = new TableRow();
                row.Cells.Add(createCell("名称：" + i.Name, false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("开始时间：" + i.StartTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("结束时间：" + i.EndTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("备注：" + i.Remark, false, Unit.Parse("25%"), HorizontalAlign.Left));
                table.Rows.Add(row);
            }

            return table;
        }

        //化疗
        private Table createChemosTable(ICollection<CureChemo> bcs)
        {
            Table table = new Table();
            table.Width = Unit.Parse("100%");

            if (bcs == null || bcs.Count < 1)
                return table;
            foreach (CureChemo bc in bcs)
            {
                TableRow row = new TableRow();
                row.Cells.Add(createCell("方案：" + ChemoSchemeTypeInfo.GetName((byte)bc.Scheme), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("开始时间：" + bc.StartTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("结束时间：" + bc.EndTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("备注：" + bc.Remark, false, Unit.Parse("25%"), HorizontalAlign.Left));
                table.Rows.Add(row);
            }

            return table;
        }

        //其他
        private Table createBasicOtherCuresTable(ICollection<CureOtherCure> ocs)
        {
            Table table = new Table();
            table.Width = Unit.Parse("100%");

            if (ocs == null || ocs.Count < 1)
                return table;
            foreach (CureOtherCure oc in ocs)
            {
                TableRow row = new TableRow();
                row.Cells.Add(createCell("名称：" + oc.Name, false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("开始时间：" + oc.StartTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("结束时间：" + oc.EndTime.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("备注：" + oc.Remark, false, Unit.Parse("25%"), HorizontalAlign.Left));
                table.Rows.Add(row);
            }

            return table;
        }

        //放疗
        private Table createRadiosTable(ICollection<CureRadio> rs)
        {
            Table table = new Table();
            table.Width = Unit.Parse("100%");

            if (rs == null || rs.Count < 1)
                return table;
            foreach (CureRadio r in rs)
            {
                TableRow row = new TableRow();
                row.Cells.Add(createCell("部位：" + RadioPlaceTypeInfo.GetName((byte)r.Place).ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("剂量：" + r.Dose.ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("次数：" + r.Times.ToString().ToString(), false, Unit.Parse("25%"), HorizontalAlign.Left));
                row.Cells.Add(createCell("备注：" + r.Remark, false, Unit.Parse("25%"), HorizontalAlign.Left));
                table.Rows.Add(row);
            }

            return table;
        }

        private TableCell createCell(String txt, Boolean haveborder, Unit width, HorizontalAlign align)
        {
            TableCell cell = new TableCell();
            cell.Text = txt;
            if (haveborder)
            {
                cell.BorderColor = System.Drawing.Color.Black;
                cell.BorderStyle = BorderStyle.Solid;
                cell.BorderWidth = Unit.Parse("1px");
            }
            if (width != null)
            {
                cell.Width = width;
            }
            cell.HorizontalAlign = align;
            return cell;
        }

        private TableCell createCell(String txt, Boolean haveborder, Unit width, HorizontalAlign align, int colspan, int rowspan)
        {
            TableCell cell = new TableCell();
            cell.Text = txt;
            if (haveborder)
            {
                cell.BorderColor = System.Drawing.Color.Black;
                cell.BorderStyle = BorderStyle.Solid;
                cell.BorderWidth = Unit.Parse("1px");
            }
            if (width != null)
            {
                cell.Width = width;
            }
            cell.HorizontalAlign = align;
            if (colspan > 1)
                cell.ColumnSpan = colspan;
            if (rowspan > 1)
                cell.RowSpan = rowspan;
            return cell;
        }

    }
}